package net.lzzy.practices.web.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Option;
import net.lzzy.practices.entities.Question;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.QuestionService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.dsig.keyinfo.KeyInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 *@author hzl on 2022/6/1.
 *Description:
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_QUESTION_ROOT)
public class QuestionApiController {
    @Autowired
    public QuestionApiController(QuestionService service) {
        this.service = service;
    }

    private final QuestionService service;

    @GetMapping(ApiConstant.ROUTE_QUESTION_CHAPTER)
    public ApiResult get(@RequestParam(name = "id")UUID chapterId,@RequestParam String token){
        boolean teacher=!AuthUtils.illegalUser(token,new int[]{User.TYPE_TEACHER});
        List<VmQuestion>questions=new ArrayList<>();
        service.get(chapterId).forEach(question -> questions.add(VmQuestion.create(question,teacher)));
        return  new ApiResult(true,questions.size()+"",questions);
    }

    private ApiResult create(JSONObject json){
        String token= json.getString(ApiConstant.KEY_TOKEN);
        String content= json.getString(ApiConstant.KEY_QUESTION_CONTENT);
        String analysis= json.getString(ApiConstant.KEY_QUESTION_ANALYSIS);
        Integer ordinal= json.getInteger(ApiConstant.KEY_QUESTION_ORDINAL);
        String strChapterId= json.getString(ApiConstant.KEY_QUESTION_CHAPTER_ID);
        Integer type= json.getInteger(ApiConstant.KEY_QUESTION_TYPE);
        if (StringUtils.isEmpty(token)||
        StringUtils.isEmpty(content)||
        StringUtils.isEmpty(analysis)||
        StringUtils.isEmpty(strChapterId)||
        ordinal==null||type==null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID chapterId=UUID.fromString(strChapterId);
        Question question=new Question();
        JSONArray array=json.getJSONArray(ApiConstant.KEY_QUESTION_OPTIONS);
        List<Option>options=new ArrayList<>();
        if (array !=null&&array.size()>0){
            for (int i=0;i<array.size();i++){
                JSONObject  jo=array.getJSONObject(i);
                Option option=new Option();
                option.setAnswer(jo.getBoolean(ApiConstant.KEY_OPTION_ANSWER));
                String oc=jo.getString(ApiConstant.KEY_OPTION_CONTENT);
                String ol=jo.getString(ApiConstant.KEY_OPTION_LABEL);
                if (StringUtils.isEmpty(oc)||StringUtils.isEmpty(ol)){
                    return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
                }
                option.setContent(oc);
                option.setLabel(ol);
                option.setQuestion(question);
                options.add(option);
            }
        }
        question.setOptions(options);
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setOrdinal(ordinal);
        question.setType(type);
        Question result=service.add(chapterId,question,token);
        if (result==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @PostMapping(ApiConstant.ROUTE_QUESTION_ADD)
    public ApiResult add(@RequestBody JSONObject json){
        return create(json);
    }

    private ApiResult update(UUID id, QuestionPutInfo info){
        String token=info.getToken();
        String content=info.getContent();
        String analysis=info.getAnalysis();
        Integer ordinal=info.getOrdinal();
        Integer type=info.getType();
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(content)||
                StringUtils.isEmpty(analysis)||
               ordinal==null||type==null || id==null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Question question=new Question();
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setOrdinal(ordinal);
        question.setType(type);
        Question result=service.update(id,question,token);
        if (result==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @PostMapping(ApiConstant.ROUTE_QUESTION_PUT)
    public ApiResult put(@PathVariable UUID id,@RequestBody QuestionPutInfo info){
        return  update(id,info);
    }

    @PostMapping(ApiConstant.ROUTE_QUESTION_OPTION_ADD)
    public ApiResult addOption(@RequestBody OptionInfo info){
        String token=info.getToken();
        String strQuestionId=info.getQuestionId();
        String content=info.getContent();
        String label=info.getLabel();
        Boolean answer=info.getAnswer();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(content) ||
                StringUtils.isEmpty(strQuestionId) ||
                StringUtils.isEmpty(label) || answer == null) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Option option=new Option();
        option.setContent(content);
        option.setLabel(label);
        option.setAnswer(answer);
        UUID questionId=UUID.fromString(strQuestionId);
        Question question=service.addOption(questionId,option,token);
        if (question==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @PostMapping(ApiConstant.ROUTE_QUESTION_OPTION_DEL)
    public ApiResult removeOption(@RequestBody OptionKeyInfo info){
        String token=info.getToken();
        String strId=info.getId();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID id=UUID .fromString(strId);
        Question question=service.removeOption(id,token);
        if (question==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @PostMapping(ApiConstant.ROUTE_QUESTION_DEL)
    public ApiResult remove(@RequestBody OptionKeyInfo info){
        String token=info.getToken();
        String strId=info.getId();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID id=UUID .fromString(strId);
        Messages msg=service.remove(id,token);
        return  new ApiResult(msg==Messages.SUCCESS, msg.toString(),null);
    }
}
